
import HeadPrefab from "../../component/HeadPrefab";
import { SpriteFrameComponent } from "../../engine/SpriteFrameComponent";
import ViewBase from "../../engine/ViewBase";
import { SocketClient } from "../../net/SocketClient";
import { GameConfig } from "../config/GameConfig";
import { RoomModel } from "../model/RoomModel";
import { RoomPlayer } from "../model/RoomPlayer";
import { UIHelper } from "../util/UIHelper";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ViewRoomPlayer extends ViewBase {

    private player: RoomPlayer;

    public onInitChild(player: RoomPlayer) {
        this.player = player;

        this.refresh()
    }

    private refresh() {
        let myPlayer: RoomPlayer = RoomPlayer.getMyPlayer()
        let head: HeadPrefab = this.getChild(this.node, "head", HeadPrefab)
        head.setHead(this.player.headimg, this.player.lv)
        let sexNode: cc.Node = this.findChild(this.node, "sexNode")
        sexNode.getComponent(cc.Sprite).spriteFrame = sexNode.getComponent(SpriteFrameComponent).spriteFrameList[this.player.sex - 1]
        let txtName: cc.Label = this.getChild(this.node, "txtName", cc.Label)
        txtName.string = this.player.nick_name
        let txtId: cc.Label = this.getChild(this.node, "txtId", cc.Label)
        txtId.string = "" + this.player.userid
        // let txtLocation: cc.Label = this.getChild(this.node, "txtLocation", cc.Label)
        // if (this.player.location) {
        //     txtLocation.string = this.player.location.split("|")[1]
        // } else {
        //     txtLocation.string = "获取不到位置"
        // }
        // let txtIp: cc.Label = this.getChild(this.node, "txtIp", cc.Label)
        // txtIp.string = this.player.ip
        let txtInvite: cc.Label = this.getChild(this.node, "txtInvite", cc.Label)
        if (this.player.invite) {
            txtInvite.string = this.player.invite + ""
        } else {
            txtInvite.string = "暂无邀请码"
        }
        for (let i = 0; i < 6; i++) {
            let magic: cc.Node = this.findChild(this.node, "iconLayout/magic" + (i + 1))
            UIHelper.onAddEvent(magic, () => {
                this.closeView();
                SocketClient.req_room_chat(RoomModel.instance.room_id, myPlayer.userid, this.player.userid, GameConfig.Chat_Type.Magic, i)
            })
        }
    }

}
